package com.huilian.iotbox.client.producer;

import com.alibaba.fastjson.JSONObject;
import com.huilian.iotbox.client.config.DelayMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

/**
 * RabbitProducer
 *
 * @Author mineChen
 * @Date 2021/9/7 下午 03:30
 */
@Slf4j
@Component
public class RabbitProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendReplayLazyQueue(String step, Object json, Integer delay) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("step", step);
        jsonObject.put("json", json);
        //发送消息时指定 header 延迟时间
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_CLIENT_LAZY_EXCHANGE, DelayMQConfig.BOX_CLIENT_LAZY_QUEUE_KEY, jsonObject.toJSONString(),
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
        log.info("--------发送sendReplayLazyQueue延迟MQ成功！--------");
    }
    public void sendVendingMachineReplayLazyQueue(String step, Object json, Integer delay) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("step", step);
        jsonObject.put("json", json);
        //发送消息时指定 header 延迟时间
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_CLIENT_LAZY_EXCHANGE, DelayMQConfig.BOX_CLIENT_vending_machine_LAZY_QUEUE_KEY, jsonObject.toJSONString(),
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(delay);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
        log.info("--------发送sendVendingMachineReplayLazyQueue延迟MQ成功！--------");
    }

    /**
     * 延迟队列，电机测试货道数量多时，分批发送
     *
     * @param delayMap
     * @param activityEndTime
     */
    public void sendMotorDetectionEndLazyQueue(Map<String, String> delayMap, int activityEndTime) {
        //发送消息时指定并指定header延迟时间
        rabbitTemplate.convertAndSend(DelayMQConfig.IOT_BOX_WAP_LAZY_EXCHANGE, DelayMQConfig.IOT_BOX_WAP_SEND_MOTOR_DETECTION_LAZY_QUEUE_KEY, delayMap,
                msg -> {
                    //设置消息持久化
                    msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                    //message.getMessageProperties().setHeader("x-delay", "6000");
                    msg.getMessageProperties().setDelay(activityEndTime);
                    return msg;
                }, new CorrelationData(UUID.randomUUID().toString()));
    }
}
